package com.carlinfo.common.util;

import com.alibaba.fastjson.JSONObject;

/**
 * 分页工具
 */
public class PageInfoUtil
{
	/*总条数*/
	private int totalRecord;
	/* 每页多少个 */
	private int pageSize = 10;
	/* 当前页 */
	private int currentPage;
	/* 总页数 */
	private int totalPage;
	/* 上一页 */
	private int prePage;
	/* 下一页 */
	private int nextPage;
	/*
	 * 当前的记录数每页的起始条数;每页10条;
	 * */
	private int currentRecord;
	public int getTotalRecord()
	{
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}
	public int getPageSize()
	{
		return pageSize;
	}
	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
	public int getCurrentPage()
	{
		if(this.currentPage < 1)
		{
			this.currentPage = 1;
		}
		if(this.getTotalRecord() > 0 &&
				this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage();
		}
		/* 如果当前 页为0就变成1 */
		if(this.currentPage ==0)
		{
			this.currentPage = 1;
		}
		return currentPage;
	}
	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}
	public int getTotalPage()
	{
		if(this.totalRecord % this.pageSize == 0)
		{
			this.totalPage = this.totalRecord / this.pageSize;
		}else
		{
			this.totalPage = this.totalRecord / this.pageSize + 1;
		}
		return totalPage;
	}
	public int getPrePage()
	{
		prePage = this.currentPage - 1;
		if(this.prePage < 1)
		{
			this.prePage = 1 ;
		}
		return prePage;
	}
	public int getNextPage()
	{
		this.nextPage = this.currentPage + 1;
		/* 当前页大于总页数 */
		if(this.getTotalPage() >= 0
				&& this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage(); 
		}
		if(this.nextPage == 0)
		{
			this.nextPage = 1;
		}
		return nextPage;
	}
	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage() - 1) * this.getPageSize();
		return currentRecord;
	}
	/**
	 * 
	{
		"currentPage":"1",
		"pageSize":"10",
		"totalRecord":"50",
		"totalPage":"20",
		"prePage":"10",
		"nextPage":"11",
		"currentRecord","11"
	}
	*/
	public String toJSON()
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* 拼装JSON */
		resultJSON.put("currentPage", this.getCurrentPage() + "");
		resultJSON.put("pageSize", this.getPageSize() + "");
		resultJSON.put("totalRecord", this.getTotalRecord() + "");
		resultJSON.put("totalPage", this.getTotalPage() + "");
		resultJSON.put("prePage", this.getPrePage() + "");
		resultJSON.put("nextPage", this.getNextPage() + "");
		resultJSON.put("currentRecord", this.getCurrentRecord() + "");
		return resultJSON.toJSONString() ; 
	}
	
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		/* 设置总条数 */
		pageInfoUtil.setTotalRecord(0);
		/* 设置一下当前页 */
		pageInfoUtil.setCurrentPage(2);
		/* 设置一下每页多少条 */
		pageInfoUtil.setPageSize(10);
		
		System.out.println("=====>" + pageInfoUtil.toJSON());
	}
	 
	
}
